﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace CMS_GYM
{
    class clsSanCauDAO
    {
        #region Attribute
        /// <summary>
        /// Kết nối SQL Server
        /// </summary>
        private CSQLServer sqlServer = new CSQLServer();
        #endregion

        /// <summary>
        /// Lấy danh sách chức năng
        /// </summary>
        public DataTable LayBang()
        {
            string sql = "sp_GetBangSanCau";
            DataTable table = sqlServer.readData(sql);
            return table;
        }

        public clsSanCauDTO LayThongTinSanCau(int SanCauId)
        {
            DataTable dt = LayBang();
            DataView dv = new DataView(dt);
            dv.RowFilter = "SanCauId=" + SanCauId.ToString();
            return ChuyenDoi(dv.ToTable());
        } 

        private clsSanCauDTO ChuyenDoi(DataTable table)
        {
            clsSanCauDTO SanCau = new clsSanCauDTO();
            if (table.Rows.Count == 1)
            {
                DataRow Dong = table.Rows[0];
                SanCau.SanCauId = int.Parse(Dong["SanCauId"].ToString());
                SanCau.MaSanCau = (Dong["MaSanCau"].ToString());
                SanCau.TenSanCau = Dong["TenSanCau"].ToString();
                SanCau.MoTa = Dong["MoTa"].ToString();
                SanCau.SoThuTu = (Dong["SoThuTu"].ToString());
                //BangGia.TrangThai = bool.Parse(Dong["TrangThai"].ToString()) == true ? 1 : 0;
                //BangGia.IsLock = bool.Parse(Dong["IsLock"].ToString()) == true ? 1 : 0;
                //BangGia.NguoiTaoId = Dong["NguoiTaoId"].ToString() == string.Empty ? 0 : int.Parse(Dong["NguoiTaoId"].ToString());
                //BangGia.NgayTao = Dong["NgayTao"].ToString() == string.Empty ? DateTime.Now : DateTime.Parse(Dong["NgayTao"].ToString());
                //BangGia.NguoiChinhSuaId = Dong["NguoiChinhSuaId"].ToString() == string.Empty ? 0 : int.Parse(Dong["NguoiChinhSuaId"].ToString());
                //BangGia.NgayChinhSua = Dong["NgayChinhSua"].ToString() == string.Empty ? DateTime.Now : DateTime.Parse(Dong["NgayChinhSua"].ToString());
                return SanCau;
            }
            return null;
        }
       /// Thêm chức năng
       /// </summary>
       /// <param name="ChucNang"></param>
       /// <returns></returns>
        public int Them(clsSanCauDTO SanCau)
        {
            int i = -1;
            string sql = "sp_InsertSanCau";
            string[] ParameterColection = new string[3] { "@MaSanCau", "@TenSanCau", "@MoTa" };
            Object[] valueofParameter = new Object[3] { SanCau.MaSanCau, SanCau.TenSanCau, SanCau.MoTa };
            i = sqlServer.writeData(sql, ParameterColection, valueofParameter);
            return i;
        }

        /// <summary>
        /// Sửa thông tin chức năng
        /// </summary>
        /// <param name="SanCau">
        /// MaChucNang  nvarchar(10)
        /// TenChucNang  nvarchar(255)
        /// </param>
        public int Sua(clsSanCauDTO SanCau)
        {
            int i = -1;
            string sql = "sp_UpdateSanCau";
            string[] ParameterColection = new string[4]{"@SanCauId","@MaSanCau","@TenSanCau","@MoTa"};
            Object[] valueofParameter = new Object[4]{SanCau.SanCauId, SanCau.MaSanCau, SanCau.TenSanCau, SanCau.MoTa};
            i = sqlServer.writeData(sql, ParameterColection, valueofParameter);
            return i;
        }

        /// <summary>
        /// Xóa chức năng
        /// </summary>
        /// <param name="MaChucNang">Mã chức năng</param>
        public int Xoa(int SanCauId)
        {
            int i = -1;
            string sql = "sp_DeleteSanCau";
            string[] ParameterColection = new string[1];
            Object[] valueofParameter = new Object[1];
            ParameterColection[0] = "@SanCauId";
            valueofParameter[0] = SanCauId;
            i = sqlServer.writeData(sql, ParameterColection, valueofParameter);
            return i;
        }
    }
}
